/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getSettingList,
  getStatisticsList,
  onGrantDispat,
  getDeliveryPaySetInfo,
  setDeliveryPaySetInfo,
  multiEditDeliveryPayStatus,
  getDeliveryPayDetails,
  manualDistributionFeeDetails,
} from '@/services/dispatStatistics';
import { message, Modal } from 'antd';
import { isArray } from '@/utils/utils';

export default {
  namespace: 'dispatStatistics',

  state: {
    settingList: {
      list: [],
      pagination: {},
    },
    statisticsList: {
      list: [],
      pagination: {},
    },
    deliveryPayDetails: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *getSettingList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getSettingList, payload);
      if (!response) return;
      const {
        code,
        data: { list, count, page },
        msg,
      } = response;

      if (code === 0 && isArray(list)) {
        yield put({
          type: 'save',
          payload: {
            settingList: {
              list,
              pagination: {
                total: count,
                current: page || 1,
                pageSize: 20,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            settingList: {
              list: [],
              pagination: {},
            },
          },
        });
        __dva_reject();
        message.error(msg);
      }
    },
    *getStatisticsList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getStatisticsList, payload);
      const { is_export, is_export_all } = payload || {};
      if (!response) return;
      const {
        code,
        data: { list, count, page },
        msg,
      } = response;
      if (code === 0) {
        if (is_export || is_export_all) {
          Modal.info({
            title: '温馨提示',
            content: '导出数据申请已提交，请至【报表下载】处下载',
          });
          return;
        }
        if (isArray(list)) {
          const formatList = list.map(item => ({
            ...item,
            start_time: payload.start_time,
            end_time: payload.end_time,
            id: item.operator_kb_code,
          }));
          yield put({
            type: 'save',
            payload: {
              statisticsList: {
                list: formatList,
                pagination: {
                  total: count,
                  current: page || 1,
                  pageSize: 20,
                  showQuickJumper: false,
                  showSizeChanger: false,
                },
              },
            },
          });
        }
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            statisticsList: {
              list: [],
              pagination: {},
            },
          },
        });

        __dva_reject();
        message.error(msg);
      }
    },
    *getDeliveryPayDetails({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const { is_export } = payload || {};
      const response = yield call(getDeliveryPayDetails, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code === 0) {
        if (is_export) {
          Modal.info({
            title: '温馨提示',
            content: '导出数据申请已提交，请至【报表下载】处下载',
          });
          return;
        }
        if (isArray(data)) {
          const formatList = data.map(item => ({
            ...item,
            start_time: payload.start_time,
            end_time: payload.end_time,
          }));
          yield put({
            type: 'save',
            payload: {
              deliveryPayDetails: {
                list: formatList,
              },
            },
          });
        }
        __dva_resolve(response);
      } else {
        yield put({
          type: 'save',
          payload: {
            deliveryPayDetails: {
              list: [],
            },
          },
        });
        __dva_reject();
        message.error(msg);
      }
    },
    *onGrantDispat({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(onGrantDispat, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code === 0) {
        message.success('设置成功');
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *manualDistributionFeeDetails({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(manualDistributionFeeDetails, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code === 0) {
        message.success('设置成功');
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *getDeliveryPaySetInfo({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getDeliveryPaySetInfo, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code === 0) {
        yield put({
          type: 'save',
          payload: {
            deliveryPaySetInfo: data,
          },
        });
        __dva_resolve(data);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *setDeliveryPaySetInfo({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(setDeliveryPaySetInfo, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code === 0) {
        message.success('设置成功');
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *multiEditDeliveryPayStatus({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(multiEditDeliveryPayStatus, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code === 0) {
        message.success('设置成功');
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
